package com;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by lizhenxing on 2018/3/5.
 */
@RestController
@RequestMapping(value = "/study")
public class StudyController {


    /**
     * 不加@RequestParam注解,不传参数, 那么接口依然能够请求抵达逻辑层.
     * 如果加了注解, 不传参数的话，请求被spring拦截，无法抵达逻辑层，直接抛400.
     * 加了注解，如果其中的参数required=false，那么不传参数，请求依旧能够抵达逻辑层.
     *
     * @param age
     * @return
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public Object getInfo(Integer age) {
        return age;
    }

    @RequestMapping(value = "/info1", method = RequestMethod.GET)
    public Object getInfo1(@RequestParam Integer age) {
        return age;
    }


    /**
     * 用GET或POST接受参数为非基本数据类型的数据。
     * 上游服务参数按照key=val形式传递即可。
     * 按照下面例子即为：
     * age=1&name=1,2
     * @param people
     * @return
     */
    @RequestMapping(value = "/body", method = RequestMethod.POST)
    public Object getBody(People people) {
        return people;
    }

    /**
     * 用POST接受json格式数据
     * 上游服务需要在body里传递json格式，如：
     * {
     *     "age":1,
     *     "name:[1,2]
     * }
     * @param people
     * @return
     */
    @RequestMapping(value = "/body1", method = RequestMethod.POST)
    public Object getBody1(@RequestBody People people) {
        return people;
    }


    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class People {
        private Integer age;
        private List<Integer> name;
    }
}

